package com.zookeeper.te3;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * 作者: LCG
 * 日期: 2020/2/26 22:33
 * 描述:
 */
public class Test04Watcher implements Watcher {

    ZooKeeper zooKeeper=new ZooKeeper("127.0.0.1:2181",10000,this);

    public Test04Watcher() throws IOException {
    }

    @Override
    public void process(WatchedEvent event) {
        String path = event.getPath();

        Event.EventType type = event.getType(); //获取事件的类型

        if(type.getIntValue()==Event.EventType.NodeChildrenChanged.getIntValue()){//子节点发生改变
            System.out.println(path+"===NodeChildrenChanged====子节点发生改变==请处理"+path);

            try {
                zooKeeper.getChildren(path,new Test04Watcher());
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }else if(type.getIntValue()== Event.EventType.NodeDataChanged.getIntValue()) {//节点的数据发生了更新改变
            System.out.println(path+"===NodeDataChanged====节点数据发生改变==请处理");
        }else if(type.getIntValue()==Event.EventType.NodeDeleted.getIntValue()){//节点被删除
            System.out.println(path+"===NodeDeleted====节点目录被删除==请处理");
        }else if(type.getIntValue()==Event.EventType.NodeCreated.getIntValue()){//节点被创建
            System.out.println(path+"===NodeCreated====节点目录被创建==请处理");
        }else if(type.getIntValue()==Event.EventType.None.getIntValue()){
            System.out.println(path+"==========None=============");
        }

    }
}
